package springcloud.test.user.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springcloud.test.common.ResultMessage;

@RestController
@RequestMapping("/hystrix")
public class HystrixController {
    private static Long MAX_SLEEP_TIME = 5000L;

    @GetMapping("/timeout")
    public ResultMessage timeout() {
        Long sleepTime = (long) (MAX_SLEEP_TIME * Math.random());
        try {
            Thread.sleep(sleepTime);
        } catch (Exception ex) {
            System.out.println("exception");
        }
        return new ResultMessage(true, "execute time:" + sleepTime);
    }

    @GetMapping("/exp/{msg}")
    public ResultMessage exp(@PathVariable("msg") String msg) {
        if ("spring".equals(msg)) {
            return new ResultMessage(true, msg);
        } else {
            throw new RuntimeException("raise exception, please check msg is spring");
        }
    }
}
